package br.com.rglariguet.rgportal.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.rglariguet.rgportal.PermissaoAcesso;
import br.com.rglariguet.rgportal.Usuario;
import br.com.rglariguet.rgportal.dao.ColaboradorDAO;
import br.com.rglariguet.rgportal.dao.PermissaoDAO;

@Controller
public class PermissaoAcessoController {
	
	@Autowired
	private PermissaoDAO permissaoDAO;
	
	@Autowired
	private ColaboradorDAO colaboradorDAO;
	
	
	@RequestMapping("/admin/listarPermissao")
	public String listarPermissao(ModelMap model, PermissaoAcesso permissao){
		List<PermissaoAcesso> permissoes = permissaoDAO.listarPermissoes();		
		model.addAttribute("permissoesList", preencherComboPermissoes(permissoes));
		model.addAttribute("permissao", new PermissaoAcesso());
		model.addAttribute("usuario", new Usuario());
		return "/admin/usuarios/novoUsuario";
	}
			
	@RequestMapping(value="/admin/salvarPermissao", method = RequestMethod.POST)
	public String salvarPermissao(ModelMap model, PermissaoAcesso permissao){
		permissaoDAO.salvar(permissao);	
		return "redirect:/admin/listarPermissao";
	}
	
	protected Map<String, String> preencherComboPermissoes(List<PermissaoAcesso> permissoes) {
		Map<String,String> permissoesMap = new LinkedHashMap<String,String>();
		for (PermissaoAcesso permissao : permissoes) {
			permissoesMap.put(permissao.getId().toString(), permissao.getGrupo());
		}
		return permissoesMap;
	}
}
